package com.cooba.adapter;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.bean.NewsLoad;
import com.cooba.utils.ACache;
import com.cooba.utils.Utils;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

public class NewsAdapter extends BaseAdapter {
    private final BitmapUtils bitmapUtils;
    private final ACache acache;
    private Context context;
    private List<NewsLoad.NewsListEntity> list;
    private final int TYPE_1 = 0;
    private final int TYPE_3 = 1;

    public List<NewsLoad.NewsListEntity> getList() {
        return list;
    }

    public void setList(List<NewsLoad.NewsListEntity> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addNewData(List<NewsLoad.NewsListEntity> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<NewsLoad.NewsListEntity> datas) {
        Log.d("wei", "" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public NewsAdapter(Context context, List<NewsLoad.NewsListEntity> list) {
        this.context = context;
        this.list = list;
        bitmapUtils = new BitmapUtils(context);
        acache = ACache.get(context);
    }

    @Override
    public int getCount() {
        if (list.size()==0){
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        String cover = list.get(position).img_url;
        String[] split = cover.split("@");
        if (split.length != 3) {
            return TYPE_1;
        }
        return TYPE_3;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
        //return super.getViewTypeCount();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        ViewHolder3 holder3 = null;
        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_1:
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.news_list_item, parent, false);
                    holder.title = (TextView) convertView.findViewById(R.id.tv_tittle);
                    holder.description = (TextView) convertView.findViewById(R.id.tv2);
                    holder.img = (ImageView) convertView.findViewById(R.id.iv);
                    convertView.setTag(holder);
                    break;
                case TYPE_3:
                    holder3 = new ViewHolder3();
                    convertView = LayoutInflater.from(context).inflate(R.layout.news_list_item3, parent, false);
                    holder3.title = (TextView) convertView.findViewById(R.id.tv_tittle);
                    holder3.iv_a = (ImageView) convertView.findViewById(R.id.iv_a);
                    holder3.iv_b = (ImageView) convertView.findViewById(R.id.iv_b);
                    holder3.iv_c = (ImageView) convertView.findViewById(R.id.iv_c);
                    convertView.setTag(holder3);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_1:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case TYPE_3:
                    holder3 = (ViewHolder3) convertView.getTag();
                    break;
            }
        }
        String isRead=acache.getAsString("readed_news_list_"+list.get(position).getId());

        switch (type) {
            case TYPE_1:
                if (list.get(position).title != null) {

                    holder.title.setText(Utils.replaceString(list.get(position).getTitle()));//标题
                }
                if (list.get(position).description != null) {
                    holder.description.setText(list.get(position).description.replaceAll("\\\\", ""));//描述
                } else {
                    holder.description.setText("");
                }

                if (!TextUtils.isEmpty(isRead)&&isRead.equals("true")){
                    holder.title.setTextColor(context.getResources().getColor(R.color.darkgray));
                }else {
                    holder.title.setTextColor(context.getResources().getColor(R.color.black));
                }
                bitmapUtils.display(holder.img,list.get(position).img_url);
                break;
            case TYPE_3:
                String cover = list.get(position).img_url;
                String[] slit = cover.split("@");
                if (list.get(position).title != null) {
                    holder3.title.setText(Utils.replaceString(list.get(position).getTitle()));//标题
                }
                if (!TextUtils.isEmpty(isRead)&&isRead.equals("true")){
                    holder3.title.setTextColor(context.getResources().getColor(R.color.darkgray));
                }else {
                    holder3.title.setTextColor(context.getResources().getColor(R.color.black));
                }
                bitmapUtils.display(holder3.iv_a, slit[0]);
                bitmapUtils.display(holder3.iv_b, slit[1]);
                bitmapUtils.display(holder3.iv_c, slit[2]);
                break;
        }

        return convertView;
    }



    class ViewHolder {
        public TextView title;
        public TextView description;
        public ImageView img;

    }

    class ViewHolder3 {
        public TextView title;
        public ImageView iv_a, iv_b, iv_c;
    }
}
